<?php
session_start();
require_once('secciones/cajasesion.php');
require_once('secciones/listaopciones.php');
require_once('secciones/footer.php');
require_once('secciones/cambiarpassword.php');





<?php

define ("MAX_SIZE","400");

$errors=0;

if($_SERVER["REQUEST_METHOD"] == "POST")
{
	$image =$_FILES["file"]["name"];
	$uploadedfile = $_FILES['file']['tmp_name'];

	if ($image)
	{
		$filename = stripslashes($_FILES['file']['name']);
		$extension = getExtension($filename);
		$extension = strtolower($extension);
		if (($extension != "jpg") && ($extension != "jpeg")

				&& ($extension != "png") && ($extension != "gif"))
		{
			echo ' Unknown Image extension ';
			$errors=1;
		}
		else
		{
			$size=filesize($_FILES['file']['tmp_name']);

			if ($size > MAX_SIZE*1024)
			{
				echo "You have exceeded the size limit";
				$errors=1;
			}

			if($extension=="jpg" || $extension=="jpeg" )
			{
				$uploadedfile = $_FILES['file']['tmp_name'];
				$src = imagecreatefromjpeg($uploadedfile);
			}
			else if($extension=="png")
			{
				$uploadedfile = $_FILES['file']['tmp_name'];
				$src = imagecreatefrompng($uploadedfile);
			}
			else
			{
				$src = imagecreatefromgif($uploadedfile);
			}

			list($width,$height)=getimagesize($uploadedfile);

			$newwidth=60;
			$newheight=($height/$width)*$newwidth;
			$tmp=imagecreatetruecolor($newwidth,$newheight);

			$newwidth1=25;
			$newheight1=($height/$width)*$newwidth1;
			$tmp1=imagecreatetruecolor($newwidth1,$newheight1);

			imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,

					$width,$height);

			imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,

					$width,$height);

			$filename = "images/". $_FILES['file']['name'];
			$filename1 = "images/small". $_FILES['file']['name'];

			imagejpeg($tmp,$filename,100);
			imagejpeg($tmp1,$filename1,100);

			imagedestroy($src);
			imagedestroy($tmp);
			imagedestroy($tmp1);
		}
	}
}
//If no errors registred, print the success message

if(isset($_POST['Submit']) && !$errors)
{
	// mysql_query("update SQL statement ");
	echo "Image Uploaded Successfully!";

}




function getExtension($str) {

	$i = strrpos($str,".");
	if (!$i) {
		return "";
	}

	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	return $ext;
}

?>











if((isset($_SESSION['idUsuario'])) && (isset($_POST['password_a'])) && (isset($_POST['password_b']))){
	// Queremos cambiar la contraseña
	$retorno = cambiarPassword($_POST['password_a'], $_POST['password_b']);
}
if((isset($_SESSION['idUsuario'])) && (isset($_POST['pic']))){
	// Queremos cambiar el avatar
	$retorno = cambiarAvatar($_POST['pic'], $_POST['password_b']);
}

?>

<!DOCTYPE HTML>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>ValenGeoArte</title>
		<script type="text/javascript" src="javascript/sha1.js"></script>
	</head>
	<body>
		<header>
		<?php 
			listaOpciones('privada');
			cajaSesion();
		?>
		</header>
		<?php 
		if(!isset($_SESSION['idUsuario'])){
		?>
		<p>
			Lo sentimos. Esta sección es únicamente para usuarios registrados y logueados.
		</p>
		<?php 
		} else {
		?>
		<ul>
			<li>
				Cambio de contraseña:
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
					<input type="password" name="password_a" id="password_a" maxlength="42" placeholder="Nueva contraseña" required="required" />
					<input type="password" name="password_b" id="password_b" maxlength="42" placeholder="Repita la nueva contraseña" required="required" />
					<input type="submit" onclick="hashContrasenyaPrivada() />
				</form>
			</li>
			<li>
				Cambio de avatar:
				<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
					<input type="file" name="pic" accept="image/png,image/jpeg,image/gif" required="required" />
					<input type="submit" />
				</form>
				El avatar se modificará en un png de 100x100 píxeles en caso de tener un formato distinto
			</li>
		</ul>
		<?php
		}
		footer(); ?>
	</body>
</html>
